package com.laolei.core.rest.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import com.laolei.core.utils.JSONUtils;

@ConfigurationProperties(prefix = HostConfig.restPrefix)
public class HostConfig implements InitializingBean {

	private static final Logger LOGGER = LoggerFactory.getLogger(HostConfig.class);

	protected static final String restPrefix = "laolei.rest";
	
	private static Set<String> basePackage;
	
	private static Map<String, Host> hosts;

	public static Host getById(String serverid) {
		Host host = hosts.get(serverid);
		if (host == null)
			LOGGER.debug("{}主机配置参数不存在，请检查配置文件application.yml中{}下的配置", serverid, restPrefix);
		return host;
	}

	public Map<String, Host> getHosts() {
		return hosts;
	}

	public void setHosts(Map<String, Host> hosts) {
		HostConfig.hosts = hosts;
	}
	
	public Set<String> getBasePackage() {
		return basePackage;
	}

	public void setBasePackage(Set<String> basePackage) {
		HostConfig.basePackage = basePackage;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (hosts == null)
			hosts = new HashMap<>();
		LOGGER.info("主机api配置的参数:{}", JSONUtils.toJSONString(hosts));
	}
}
